import socket


def main():
    # 创建TCP套接字
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 连接服务器
    server_ip = input("请输入要连接的服务器IP：")
    server_port = int(input("请输入连接服务器的PORT："))

    server_addr = (server_ip, server_port)
    tcp_socket.connect(server_addr)
    # 发送/接受数据
    while True:
        send_data = input("请输入要发送的数据：")
        if send_data:
            tcp_socket.send(send_data.encode("gbk"))
            recv_data = tcp_socket.recv(1024).decode("gbk")
            print(recv_data)
        else:
            break
    # 关闭套接字
    tcp_socket.close()


if __name__ == "__main__":
    main()
